<?php

/*
 * This file is part of PHP CS Fixer.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace app\common\lib;

//use app\index\job\Recharge;
use app\common\job\DelayCoupon;
use app\common\job\order\Award;
use think\facade\Log;
use think\Queue;

class Job
{
    protected $classArr = [
        /*************************************微信推送相关队列********************************/
        //图文消息
//        'text'                        => 'app\common\job\wechat\Text',
        //扫码
        'scan'                        => 'app\common\job\wechat\Scan',
        //关注
        'subscribe'                   => 'app\common\job\wechat\Subscribe',
        //取关
//        'unsubscribe'                 => 'app\common\job\wechat\Unsubscribe',
        //领卡
        'user_get_card'               => 'app\common\job\wechat\UserGetCard',
        //提交激活信息
        'submit_membercard_user_info' => 'app\common\job\wechat\SubmitMemberCardUserInfo',

        /*************************************微信推送相关队列********************************/

        /*************************************订单相关队列********************************/
        //发货队列
        'join_shipping'               => 'app\common\job\order\Shipping',
        //礼品赠送队列
        'q_order_award'    => Award::URL,
        //券延期队列
        'q_coupon_delay'    => DelayCoupon::URI,


        /*******************************************************************************/
        //模板消息推送
        'send_temp_message'           => 'app\common\job\message\Temp',
    ];

    /**
     * @Desc: 消息队列生产者
     *
     * @param $class        string|array  消费类
     * @param $jobName      string 任务名称
     * @param $jobData      array 任务参数提
     * @param $delay_time   int 延迟时间/秒
     *
     * @return bool
     * @User: Randy_Chen
     * @Date: 2019/4/25
     * @Time: 10:27
     */
    public function actionWithJob($class, $jobName, $jobData, $delay_time = 0)
    {
        //无消费类不进入队列
        if (!isset($this->classArr[$class])) {
            return true;
        }

        $jobHandlerClassName = isset($this->classArr[$class]) ? $this->classArr[$class] : $class;
        $jobQueueName = $jobName;

        if ($delay_time > 0) {
            //延迟执行;
            $isPushed = Queue::later($delay_time,$jobHandlerClassName, $jobData, $jobQueueName);
        } else {
            //立即执行
            $isPushed = Queue::push($jobHandlerClassName, $jobData, $jobQueueName);
        }
        if (false !== $isPushed) {
            return true;
        }

        return false;
    }
}
